A cell array is a special kind of array where the elements are cells that can contain other MATLAB arrays. For example, one cell of a cell array might contain a real matrix; another cell might contain a text string array; while a third cell might contain a vector of complex values. You can generate cell arrays of any size and shape. Structures and cell arrays provide a systematic storage mechanism for different types of data. They primarily differ in the way they organize data: in structures, data is accessed from named fields; in cell arrays, data is accessed through matrix indexing operations.
Creating Cell Arrays
You can create cell arrays in the following ways.
1. Creating Cell Arrays via Assignment Statements
Generate a cell array by assigning values to individual cells, assigning one cell at a time. Depending on the assignment, an array of a certain size is automatically generated. There are two ways to assign values to cells:
Cell Indexing: Place the cell index number inside parentheses in standard array notation, and place the content of the cell on the right side of the assignment statement, enclosed in curly braces.
[Example 2-79] Create a 2×2 cell array A:
A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};
A(1,2) = {'Liu zhong'};
A(2,1) = {3+7i};
A(2,2) = {-pi:pi/10:pi};
Note: {} represents an empty cell array, just as [] represents an empty numeric array matrix. You can use empty cell arrays in any cell array assignment.
Content Indexing: Enclose the cell index number with curly braces in standard array notation on the left side, and specify the cell content on the right side of the assignment statement.
A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A{1,2} = 'Liu zhong';
A{2,1} = 3+7i;
A{2,2} = -pi:pi/10:pi;
Note: If you already have a numeric array with a given name, do not try to create a cell array with the same name without clearing the numeric array first. Because without clearing the numeric array, MATLAB will think you are confusing cell and numeric syntax and will generate an error.
MATLAB displays cell arrays in a compressed form:
A =
[3x3 double] 'Liu zhong'
[3.0000+ 7.0000i] [1x21 double]
To display the full content of cells, you can use the celldisp function. For advanced graphical display of cell structures, you can use the cellplot function. If you assign a value to a cell with a dimension greater than the current array dimension, MATLAB will automatically expand the array to include the specified index number. It will fill all intervening cells with empty matrices. For example, the following assignment puts a 2×2 cell array A into a 3×3 cell array.
A(3,3) = {5};
2. Pre-allocating Cell Arrays Using the cell Function
Use the cell function to pre-allocate an empty cell array of a specified size. The following statement creates an empty cell array:
B = cell(2,3);
Fill B cells with assignment statements below:
B(1,3) = {1:3};
Getting Data from Cell Arrays
You can get data from cell arrays, either storing the results as standard arrays or as cell arrays. This section introduces:
Using content indexing to get cell content;
Using cell indexing to get cell subsets.
1. Using Content Indexing to Get Cell Content
You can use content indexing on the right side of a statement to get some or all data from a single cell. Specify a variable on the left side of the assignment statement to get the content of the cell. Enclose the cell index expression on the right side with curly braces.
[Example 2-80]
For the cell array N below:
N{1,1} = [1 2; 4 5];
N{1,2} = 'Name';
N{2,1} = 2-4i;
N{2,2} = 7;
You can get the string in N{1,2} using c=N{1,2}, i.e.
c = N{1,2}
c =
Name
Note: When assigning values, you can use content indexing to get the content within a single cell, not the content within a cell subset. For example, statements like A{1,:} = value and B = A{1,:} are not legal. However, you can use cell subsets anywhere in a comma-separated variable list.
2. Using Cell Indexing to Get Cell Subsets
Use cell indexing to assign entire sets of cells to another variable, creating a new cell array. Use the colon operator to get cell subsets from a cell array.
Deleting Cells and Reshaping Cell Arrays
You can delete entire dimensions of cells with a single statement, just like deleting standard arrays. You can use vector index numbers when deleting rows or columns of cells. Assign an empty matrix to that dimension. For example:
A(2)=[]
When deleting cells, curly braces do not appear in the assignment statement.
You can use the reshape function to reshape cell arrays. The number of cells must be the same after reshaping; you cannot add or delete cells using the reshape function.
[Example 2-81]
The command line below first creates a cell array A, then uses the reshape function to change its size.
A = cell(3,4);
size(A)
ans =
3 4
B = reshape(A,6,2);
size(B)
ans =
6 2
Applying Functions and Operators
Using indexing, you can apply functions and operators to the content within cells.
[Example 2-82] Use content indexing to call a function that takes cell content as a parameter.
A{1,1} = [1 2; 3 4];
A{1,2} = randn(3,3);
A{1,3} = 1:5;
B = sum(A{1,1})
B =
4 6
To apply a function to cells in multiple non-nested cell arrays, you can use a loop structure. For example:
for k = 1:length(A)
M{k} = sum(A{1,k});
end
Cell Array Nesting
Cells of a cell array can contain another cell array, or an array with cell arrays as elements. You can create nested cell arrays using nested curly braces in the cell function or assignment statements. You can then access and manipulate individual cells, cell subarrays, or cell elements.
1. Creating Nested Arrays Using Nested Curly Braces
[Example 2-83] You can create a nested cell array through paired curly braces.
Enter in the Command Window:
clear A
A(1,1) = {magic(5)};
A(1,2) = {{[5 2 8; 7 3 0; 6 7 3] 'Test 1'; [2-4i 5+7i] {17 []}}}
A =
[5x5 double] {2x2 cell}
Note: The right side of the statement is enclosed by two pairs of curly braces. The first pair represents cell (1,2) of the cell array A, and the second pair includes a cell array inside the outer cell.
2. Generating Nested Arrays Using the cell Function
Use the cell function to nest cell data, assigning the output value of the cell to an existing cell.
[Example 2-84] Create an empty cell array: A=cell(1,2); Create a cell array in A(1,2): A(1,2)={cell(2,2)}; Fill A with assignments.
A(1,1) = {magic(5)};
A{1,2}(1,1) = {[5 2 8; 7 3 0; 6 7 3]};
A{1,2}(1,2) = {'Test 1'};
A{1,2}(2,1) = {[2-4i 5+7i]};
A{1,2}(2,2) = {cell(1,2)};
A{1,2}{2,2}(1) = {17};
The first set of index numbers accesses the top-level cell, and subsequent ones access data in deeper cells. For example: array A has 3 layers of nesting. To get the numeric value in cell (1,1), use A{1,1}; to get the array at position (1,1) in cell (1,2), use A{1,2}{1,1}; to get the array in cell (1,2), use A{1,2}; to get the empty cell at position (2,2) in cell (1,2), use A{1,2}{2,2}{1,2}.
Converting Between Cell Arrays and Numeric Arrays
You can convert between cells and numeric arrays using loops.
[Example 2-85] Create a cell array F as follows:
F{1,1} = [1 2; 3 4];
F{1,2} = [-1 0; 0 1];
F{2,1} = [7 8; 4 1];
F{2,2} = [4i 3+2i; 1-8i 5];
Now use 3 sets of loops to copy the contents below into the numeric array NUM.
for k = 1:4
for m = 1:2
for n = 1:2
NUM(m,n,k) = F{k}(m,n);
end
end
end
Similarly, you must use loops to assign each value in the numeric array to a cell in the cell array.
G = cell(1,16);
for m = 1:16
G{m} = NUM(m);
End
String Cell Arrays
Saving multiple sets of strings as cell arrays avoids frequently adding spaces after short strings. Using a series of functions provided by MATLAB, you can perform two operations on string cell arrays: one is converting between standard character arrays and string cell arrays, and the other is performing string comparison operations on string cell arrays.
[Example 2-86] The cellstr function can convert a string to a string cell array.
For the string
data = ['Sun junfang';'NewStudent';'Beijing '];
This matrix pads each row with spaces to make them the same length.
Now use the cellstr function to create a column vector of cells, each cell containing a string in the data array.
celldata = cellstr(data)
celldata =
'Sun junfang'
'NewStudent'
'Beijing'
Note: The cellstr function removes the spaces added for padding in each row. For example:
length(celldata{3})
ans =
7
The iscellstr function determines if the input variable is a string cell array. It returns a logical value true (1) or false (0).
iscellstr(celldata)
ans =
1
Structure Cell Arrays
[Example 2-87] Use cell arrays to store sets of structures with different fields.
Create the structure cell array c_str.
c_str = cell(1,2);
c_str{1}.label = ' - 12/5/94';
c_str{1}.obs = [47 52 55 48; 17 22 35 11];
c_str{2}.xdata = [-0.03 0.41 1.98 2.12 17.11];
c_str{2}.ydata = [-3 5 18 0 9];
c_str{2}.zdata = [0.6 0.8 1 2.2 3.4];
Cell 1 of the c_str array contains a structure with two fields, one being a string and the other a vector. Cell 2 contains a structure with 3 vector fields. When creating a cell array of structures, you must use content indexing. Similarly, you must use content indexing to get the content of the structure in the cell. The syntax for content indexing is:
cell_array{index}.field
For example, to get the label field of the structure in cell 1, use c_str{1}.label.
Multidimensional Cell Arrays
Similar to numeric arrays, multidimensional cell arrays are an extension of the two-dimensional cell array model. You can generate multidimensional cell arrays using the cat function.
[Example 2-88] Create a simple three-dimensional cell array C.
A{1,1} = [1 2;4 5];
A{1,2} = 'Name';
A{2,1} = 2-4i;
A{2,2} = 7;
B{1,1} = 'Name2';
B{1,2} = 3;
B{2,1} = 0:1:3;
B{2,2} = [4 5]';
C = cat(3,A,B);